// 路由文件 路由创建，路由模式 #hash
import { createRouter, createWebHashHistory } from 'vue-router'

// 配置路由
const route = createRouter({
    history: createWebHashHistory(), // 路由模式 #hash
    routes: [  // 配置路径
        {
            path: '/',
            redirect: '/login'
        },
        {
            path: '/home',
            component: () => import('../views/Home.vue'),
            meta: {
                isAuth: true  // 是否需要鉴权，判断是否登录
            }
        },
        {
            path: '/login',
            component: () => import('../views/Login.vue')
        },
        {
            path: '/register',
            component: () => import('../views/Register.vue')
        }
    ]
})

// 路由前置守卫
route.beforeEach((to, from, next) => {
    if(to.meta.isAuth){
        if(localStorage.getItem('token')){
            next()
        }else{
            // alert('请先登录')
            ElMessage.error('请先登录')
        }
    }else{
        next()
    }
})

// 导出路由
export default route